import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { HeroService } from '../../../../services/hero.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import {
  FormBuilder,
  FormControl,
  FormGroup,
  Validators
} from '@angular/forms';

@Component({
  selector: 'app-editmaint',
  templateUrl: './editmaint.component.html',
  styleUrls: ['./editmaint.component.css']
})
export class EditmaintComponent implements OnInit {

  validateForm: FormGroup;
  btn_loading = false;

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
  }

  constructor(private heroService: HeroService,
    private fb: FormBuilder,
    private router: Router,
    private route: ActivatedRoute,
    private message: NzMessageService,
    private modalService: NzModalService) { }

  phoneNUmberValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (!(/^1\d{10}$/.test(control.value))) {
      return { confirm: true, error: true };
    }
  }

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      name: [null, [Validators.required]],
      phoneNumberPrefix: ['+86'],
      phoneNumber: [null, [Validators.required]],
      type: [null, [Validators.required]]
    });
    const id = this.route.snapshot.paramMap.get('id');
    let url = host + "/main/merchantConnectMaint/view?id=" + id;
    this.heroService.get(url).then(result => {
      if (result.code == 0) {
        this.validateForm.get('name').setValue(result.data.name);
        this.validateForm.get('phoneNumber').setValue(result.data.mobile);
        this.validateForm.get('type').setValue(String(result.data.type));
      } else {
        this.modalService.error({
          nzTitle: '提示',
          nzContent: result.msg
        });
      }
    })
  }

  editMaint() {
    if (this.validateForm.status == "VALID") {
      this.btn_loading = true;
      const id = this.route.snapshot.paramMap.get('id');
      let url = host + "/main/merchantConnectMaint/save?id=" + id;
      let model = 'name=' + this.validateForm.value.name + '&mobile=' + this.validateForm.value.phoneNumber + '&type=' + this.validateForm.value.type;
      this.heroService.post(url, model).then(result => {
        this.btn_loading = false;
        if (result.code == 0) {
          this.btn_loading = false;
          this.message.success('修改成功');
          this.router.navigate(['/main/maintlist']);
        } else {
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
      })
    }
  }

  checkMobile() {
    this.validateForm.get('phoneNumber')!.setValidators(this.phoneNUmberValidator);
    this.validateForm.get('phoneNumber')!.markAsDirty();
    this.validateForm.get('phoneNumber')!.updateValueAndValidity();
  }
  clearCheckMobile() {
    this.validateForm.get('phoneNumber')!.clearValidators();
    this.validateForm.get('phoneNumber')!.markAsPristine();
    this.validateForm.get('phoneNumber')!.updateValueAndValidity();
  }
  backPrevent() {
    return false;
  }
}
